package com.xjk.shiro.service.impl;

import com.xjk.shiro.entity.User;
import com.xjk.shiro.entity.UserRoles;
import com.xjk.shiro.mappers.UserMapper;
import com.xjk.shiro.service.UserService;
import com.xjk.shiro.utils.RandomSaltUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: XJK
 */
@Service("UserService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public void registeredUser(User user) {
        String salt = RandomSaltUtil.getSalt(10);
        user.setSalt(salt);
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 2048);
        user.setPassword(md5Hash.toHex());
        userMapper.insertUser(user);
    }

    @Override
    public User findByUsername(String username) {

        return userMapper.findUserByUsername(username);
    }

    @Override
    public UserRoles findRoleByUsername(String username) {
        return userMapper.findUserRolesByUsername(username);
    }
}
